/**
 * Android FriendFeed Client
 * Copyright (C) 2010 E & B Labs
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.eblabs.androidfriendfeedclient;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.eblabs.friendfeed.ApiException;
import net.eblabs.friendfeed.UnAuthorizedException;

/* {@inheritDoc}. */
public class FriendfeedAdapter extends ArrayAdapter<FriendfeedEntry> {
    /**
     * Variable que contiene el conexto.
     * @type   Context
     * @access private
     */
    private Context mContext;
    /**
     * Contenedor de entradas.
     * @type   ArrayList<FriendfeedEntry>
     * @access private
     */
    private ArrayList<FriendfeedEntry> items;

    /**
     * Constructor de la clase.
     * @param context
     * @param textViewResourceId
     * @param items
     */
    public FriendfeedAdapter(final Context context,
            final int textViewResourceId,
            final ArrayList<FriendfeedEntry> items) {
        super(context, textViewResourceId, items);
        this.items = items;
        this.mContext = context;
    }

    /**
     * Método que se lanza para visualizar lo fila.
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(final int position, final View convertView,
            final ViewGroup parent) {
        View v = convertView;

        if (v == null) {
            LayoutInflater vi = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.friendfeedentryrow, null);
        }

        FriendfeedEntry entry = items.get(position);

        if (entry != null) {
            ImageView picture = (ImageView) v.findViewById(R.id.picture);

            TextView from = (TextView) v.findViewById(R.id.from);
            TextView body = (TextView) v.findViewById(R.id.body);
            TextView date = (TextView) v.findViewById(R.id.date);
            TextView nComments = (TextView) v.findViewById(R.id.ncomments);

            date.setTextSize(10);
            nComments.setTextSize(10);

            try {
                picture.setImageBitmap(entry.getPicture());
            } catch (IOException ex) {
                Logger.getLogger(FriendfeedAdapter.class.getName())
                        .log(Level.SEVERE, null, ex);
            } catch (UnAuthorizedException ex) {
                Logger.getLogger(FriendfeedAdapter.class.getName())
                        .log(Level.SEVERE, null, ex);
            } catch (ApiException ex) {
                Logger.getLogger(FriendfeedAdapter.class.getName())
                        .log(Level.SEVERE, null, ex);
            }

            if (from != null) {
                from.setText(entry.getFrom());
            }

            if (body != null) {
                body.setText(entry.getBody());
            }

            if (date != null) {
                //date.setText(R.string.send_at + entry.getDate());
                String str = new StringBuffer().append(R.string.send_at)
                        .append(entry.getDate()).toString();
                date.setText(R.string.send_at);
            }

            if (nComments != null) {
                nComments.setText("Comentarios: " + entry.getNComments());
            }
        }

        return v;
    }
}